package com.example.controller;

import com.example.dao.UserDAO;
import com.example.model.SystemAdmin;
import com.example.model.ManagementUser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    private UserDAO userDAO = new UserDAO();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求和响应的编码格式为UTF-8
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String confirmPassword = request.getParameter("confirmPassword");
        String name = request.getParameter("name");
        String contactInfo = request.getParameter("contactInfo");
        String role = request.getParameter("role");

        System.out.println("role");

        if (password == null || !password.equals(confirmPassword)) {
            request.setAttribute("error", "两次输入的密码不一致");
            request.getRequestDispatcher("jsp/register.jsp").forward(request, response);
            return;
        }

        switch (role) {
            case "系统管理员": {
                SystemAdmin admin = new SystemAdmin();
                admin.setUsername(username);
                admin.setPassword(password);
                admin.setName(name);
                admin.setContactInfo(contactInfo);
                boolean registerSuccess = userDAO.registerSystemAdmin(admin);
                if (registerSuccess) {
                    request.setAttribute("success", "注册成功！请登录");
                    request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
                } else {
                    request.setAttribute("error", "注册失败");
                    request.getRequestDispatcher("jsp/register.jsp").forward(request, response);
                }
                break;
            }
            case "用户": {
                ManagementUser user = new ManagementUser();
                user.setUsername(username);
                user.setPassword(password);
                user.setName(name);
                user.setDepartmentId(1); // 默认部门id，可以根据实际情况修改
                user.setContactInfo(contactInfo);
                boolean registerSuccess = userDAO.registerManagementUser(user);
                if (registerSuccess) {
                    request.setAttribute("success", "注册成功！请登录");
                    request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
                } else {
                    request.setAttribute("error", "注册失败");
                    request.getRequestDispatcher("jsp/register.jsp").forward(request, response);
                }
                break;
            }
            default:
                request.setAttribute("error", "请选择正确的角色");
                request.getRequestDispatcher("jsp/register.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("jsp/register.jsp").forward(request, response);
    }
}